<?php
/**
 * Created by PhpStorm.
 * User: changyifan
 * Date: 2018/3/2
 * Time: 上午10:19
 */

namespace Home\Model;
use Think\Model;
use Think\Model\BaseModel;

class StuTcModel extends BaseModel {


    protected $_link = array(
        'Student' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Student',
            'foreign_key' => 'stu_id',
            'mapping_fields' => 'stu_name,stu_sex,cla_id',
            'as_fields' => 'stu_name,stu_sex,cla_id'
        ),
        'Student_attendance' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Student',
            'foreign_key' => 'stu_id',
            'mapping_fields' => 'stu_name',
            'as_fields' => 'stu_name'
        ),
        'Naturalclass' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Naturalclass',
            'foreign_key' => 'cla_id',
            'mapping_fields' => 'pro_id',
            'as_fields' => 'pro_id'
        ),
        'Profession' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Profession',
            'foreign_key' => 'pro_id',
            'mapping_fields' => 'pro_name',
            'as_fields' => 'pro_name'
        ),
        'Teachingclass' => array(
            'mapping_type' => self::BELONGS_TO,
            'class_name' => 'Teachingclass',
            'foreign_key' => 'tc_id',
            'mapping_fields' => 'tc_name',
            'as_fields' => 'tc_name'
        ),
    );

    protected $_scope = array(
      'default' => array(
          'where' => array('tc_id' => ''),
          'field' => array('stu_id','stu_tc_status','tc_id'),
      ),
      'attendance' => array(
          'where' => array('tc_id' => ''),
          'field' => array('stu_id','numlogin','sumstudytime','score_post'),
      ),
    );

    public function getStuTcInfoListWithTcId($tc_id){

        if ($tc_id == 0){
            $teachingclass = D('Teachingclass','Model');
            $tc_info = $teachingclass->getTeachingClassInfoListByAll();
            $con = array();
            foreach ($tc_info as $key => $value){
                $con[$key] = $value['tc_id'];
            }
            $tc_id = array('in',$con);
        }
        $result = $this-> scope('default',array('where'=>array('tc_id'=>$tc_id))) -> relation(true) -> select();
        if($result){
            foreach($result as $key => $value){
                if ($value['stu_tc_status'] == 0){
                    $result[$key]['stu_tc_status'] = '已通过';//不需要申请的
                }else if($value['stu_tc_status'] == 1){
                    $result[$key]['stu_tc_status'] = '已申请';
                }else{
                    $result[$key]['stu_tc_status'] = '已通过';
                }
            }
            return $result;
        }else{
            return array();
        }
    }
    public function getAttendanceStuTcInfoListWithTcId($tc_id,$nc_id,$stu_tc_status){
//        var_dump($nc_id);
        if ($tc_id == 0){
            $tc_id = array('like','%%');
        }
        if ($stu_tc_status == 0){
            $stu_tc_status = array('like','%%');
        }
        if ($nc_id != 0){
            $student = D('Student','Model');
            $stu_info = $student->where(array('cla_id'=>$nc_id))->field('stu_id')->select();

            $con = array();
            foreach ($stu_info as $key => $value){
                $con[$key] = $value['stu_id'];
            }
            $stu_id = array('in',$con);
            $result = $this-> scope('attendance',array('where'=>array('tc_id'=>$tc_id,'stu_id'=>$stu_id,'stu_tc_status'=>$stu_tc_status))) -> relation('Student_attendance') -> select();
        }else{
            $result = $this-> scope('attendance',array('where'=>array('tc_id'=>$tc_id,'stu_tc_status'=>$stu_tc_status))) -> relation('Student_attendance') -> select();
        }
        if($result){
            foreach($result as $key => $value){
//                if ($value['stu_tc_status'] == 0){
//                    $result[$key]['stu_tc_status'] = '不需要申请';
//                }else if($value['stu_tc_status'] == 1){
//                    $result[$key]['stu_tc_status'] = '已申请';
//                }else{
//                    $result[$key]['stu_tc_status'] = '已通过';
//                }
                $result[$key]['status'] = '在线';
            }
            return $result;
        }else{
            return array();
        }
    }

}